package com.itheima.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.eneity.Dish;
import com.itheima.eneity.DishFlavor;
import com.itheima.eneity.Setmeal;
import com.itheima.eneity.SetmealDish;
import com.itheima.mapper.SetmealMapper;
import com.itheima.serivce.SetmealDishSerivce;
import com.itheima.serivce.SetmealSerivce;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealSerivceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealSerivce {

    @Autowired
    private SetmealDishSerivce setmealDishSerivce;

    /**
     * 新增套餐
     * @param setmealDto
     */
    @Transactional
    public void saveWithFlavor(SetmealDto setmealDto) {
//保存套餐的基本信息到套餐表dish
        this.saveWithFlavor(setmealDto);

        //套餐ID
        Long dishId = setmealDto.getId();

        //套餐口味
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setDishId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐口味数据到套餐口味表dish_flavor
        setmealDishSerivce.saveBatch(setmealDishes);
    }
    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        //查询套餐基本信息，从dish表查询
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前套餐对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> flavors=setmealDishSerivce.list(queryWrapper);
        setmealDto.setSetmealDishes(flavors);
        return setmealDto;
    }

    /**
     * 修改套餐信息和对应的口味信息
     * @param setmealDto
     */
    @Transactional
    public void updateWithFlavor(SetmealDto setmealDto) {
//更新dish表基本信息
        this.updateById(setmealDto);

        //清理当前套餐对应口味数据----dish_flavor表的delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishSerivce.remove(queryWrapper);

        //添加当前提交过来的套餐口味数据----dish_flavor表的insert操作
        List<SetmealDish> flavors=setmealDto.getSetmealDishes();

        flavors=flavors.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishSerivce.saveBatch(flavors);
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    @Transactional
    public void remove(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count1 = this.count(queryWrapper);
        if (count1>0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中不能删除，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据
        setmealDishSerivce.remove(lambdaQueryWrapper);
    }
}
